package rui.game;

import lombok.Setter;
import rui.game.component.*;
import rui.game.component.function.Cheat;
import rui.game.component.function.Exit;
import rui.game.component.function.Restart;
import rui.game.listerner.KeyType;
import rui.game.listerner.MouseMotion;
import rui.game.listerner.WindowClose;
import rui.game.move.MoveStrategy;

import javax.swing.*;
import java.awt.event.WindowListener;

/**
 * 抽象的 容器。
 *
 * @author 赵睿
 */
@Setter
public abstract class AbstractContext {
    private GameFrame gameFrame;
    private GamePanel gamePanel;


    protected Scope scope;
    protected Count count;

    private Restart restart;
    private Exit exit;
    private Cheat cheat;

    private GameMenu menu;
    private Function function;

    private Center center;
    private JHead jHead;

    private MoveStrategy moveStrategy = new MoveStrategy();

    private WindowListener closeListener;
    private MouseMotion mouseMotion;
    private KeyType keyType;


    /**
     * 核心类，刷新此容器
     */
    public void refresh() {
        prepareRefresh();
        onRefresh();
        publishEvent();
    }

    /**
     * 启动准备
     */
    public void prepareRefresh() {
        initResource();
    }

    /**
     * 启动
     */
    public void onRefresh() {

        count.setCount(0);


        gamePanel.refresh();

        //TODO 随机数
//        SjsRule(btnary);// 再执行随机数的方法

        //TODO 填空
//        Labelcount.setText("0");
        //TODO 渲染
//        changeColor(btnary);


    }

    public void initResource() {
        if (gameFrame == null) {
            gameFrame = new GameFrame();
        }

        cheat = Cheat.init(cheat, (actionEvent) -> cheat());
        restart = Restart.init(restart, gameFrame, (actionEvent) -> onRefresh());
        exit = Exit.init(exit, gameFrame, (e) -> exit());

        scope = Scope.init(scope);
        count = Count.init(count);

        gamePanel = GamePanel.init(gamePanel);
        menu = GameMenu.init(menu, cheat, gameFrame);

        center = Center.init(center, gamePanel, gameFrame);
        jHead = JHead.init(jHead, gameFrame, scope, count, cheat);
        function = Function.init(function, gameFrame);


        initKey();
        initMouse();
        initClose();

        gameFrame.init(keyType, mouseMotion, closeListener);


    }


    private void initMouse() {
        if (mouseMotion == null) {
            mouseMotion = new MouseMotion();
        }
    }

    private void initKey() {
        if (keyType == null) {
            keyType = new KeyType(moveStrategy);
        }

    }


    protected void initClose() {
        if (closeListener == null) {
            closeListener = new WindowClose(exit);
        }
    }


    public void publishEvent() {

    }


    protected void cheat() {
        gamePanel.getChunkMap().get(3.3).setText("1024");
        gamePanel.getChunkMap().get(2.3).setText("1024");
        //TODO 随机数
        //TODO  渲染
//            SjsRule(btnary);
//            changeColor(btnary);
    }

    public void exit() {
        if (JOptionPane.showConfirmDialog(null, "您真的打算退出吗？"
                , "退出程序", JOptionPane.YES_NO_OPTION
                , JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }


}
